home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5745 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Structure member offsets at compile time - help!
  5. Date: Tue, 06 Feb 1996 18:05:26 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <31177C46.6554@cmt.lpr.mail.carel.fi>
  8. References: <4f6qt5$d16@maureen.teleport.com>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Jeff Grossman wrote:
  16. > I'm looking for a way to get an int offset to a structure member
  17. > at compile time.
  18. > The reason is that I need to build a table of addresses of structure
  19. > members (if you must know, its for simplifying the process of doing
  20. > hundreds of ODBC SQLBindCol calls).
  21. > Right now I can create a table with addresses that point to a structure
  22. > on the stack. I cannot use a static structure or a heap structure
  23. > for these reasons:
  24. > 1) if the structure is static, it is not thread safe. If I protect
  25. > it with a critical section, it introduces too much serialization into
  26. > the application
  27. > 3) if the structure is on the heap, there is no way to create a compile
  28. > time table (if I could figure that one out, I wouldn't need to post
  29. > this question <g>! )
  30. > The drawback to the existing method is the huge amount of code generated
  31. > to fill this structure at each procedure entry, If I had a compile
  32. > time static structure with int offsets, I could add in the actual
  33. > structure address as I needed.
  34. > Unfortunately, nothing I've tried works. I even tried a kludges such as:
  35. > (char)(&Struc.item) - (char)(&Struc)
  36. > This gives me int offsets. However, the compiler is not smart enough
  37. > (even with optimization) to figure out that this is a compile time
  38. > constant and generates code to do the pointer arithmetic.
  39. > Does anyone know a trick to get compile time structure offsets?
  40. > TIA.
  41. > Jeff
  42.  
  43. Check out the 'offsetof' macro declared in <stddef.h>.
  44.  
  45. Later,
  46.  AriL
  47.  
  48. -- 
  49. All my opinions are mine and mine alone.
  50.